import 'package:flutter/material.dart';

/// 旋转组件
class RotateWidget extends StatefulWidget {
  final Widget child;
  final double angle;
  final double endAngle;
  final int duration;
  final Curve? curve;
  final Alignment alignment;
  final Offset? origin;
  final bool transformHitTests;
  final FilterQuality? filterQuality;
  final bool animate;

  //循环播放
  final bool repeat;

  // 反向
  final bool reverse;

  const RotateWidget({
    super.key,
    required this.child,
    //值是弧度   π/2, π, 3π/2
    this.angle = 0.0,
    //值是弧度   π/2, π, 3π/2
    this.endAngle = 0.0,
    this.duration = 300,
    this.curve,
    this.alignment = Alignment.center,
    this.origin,
    this.transformHitTests = true,
    this.filterQuality,
    this.animate = false,
    this.repeat = false,
    this.reverse = false,
  });

  @override
  _RotateWidgetState createState() => _RotateWidgetState();
}

class _RotateWidgetState extends State<RotateWidget> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(milliseconds: widget.duration),
      vsync: this,
    );
    createAnimation();
    _start();
  }

  void createAnimation() {
    _animation = Tween<double>(begin: widget.angle, end: widget.endAngle).animate(
      widget.curve == null ? _controller : CurvedAnimation(parent: _controller, curve: widget.curve!),
    );
  }

  void _start() {
    if (widget.animate) {
      if (widget.repeat) {
        _controller.repeat(reverse: widget.reverse);
      } else {
        _controller.forward();
      }
    }
  }

  @override
  void didUpdateWidget(RotateWidget oldWidget) {
    super.didUpdateWidget(oldWidget);

    // 当旋转角度、持续时间或动画曲线发生变化时重置动画
    if (widget.animate) {
      if (widget.angle != oldWidget.angle || widget.endAngle != oldWidget.endAngle) {
        createAnimation();
      }
      if (widget.angle != oldWidget.angle || widget.duration != oldWidget.duration || widget.curve != oldWidget.curve || widget.repeat != oldWidget.repeat || widget.reverse != oldWidget.reverse) {
        _controller
          ..duration = Duration(milliseconds: widget.duration)
          ..reset();
        _start();
      }
    } else {
      _controller.stop();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.animate) {
      // 动画模式下使用AnimatedBuilder构建旋转动画
      return AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          return Transform.rotate(
            angle: _animation.value,
            alignment: widget.alignment,
            origin: widget.origin,
            transformHitTests: widget.transformHitTests,
            filterQuality: widget.filterQuality,
            child: child,
          );
        },
        child: widget.child,
      );
    } else {
      // 非动画模式下直接应用旋转变换
      return Transform.rotate(
        angle: widget.angle,
        alignment: widget.alignment,
        origin: widget.origin,
        transformHitTests: widget.transformHitTests,
        filterQuality: widget.filterQuality,
        child: widget.child,
      );
    }
  }
}
